//DIFFERENT SAMPLES: 
	//LEVEL 0 = ALL STUDENTS; 
	//LEVEL 1 = STUDENTS IN SESSIONS WITH < 25% MISSING IN ENDLINE;
	//LEVEL 2 = STUDENTS IN SESSIONS WITH < 20% MISSING IN ENDLINE;
	//LEVEL 3 = STUDENTS IN SESSIONS WITH < 15% MISSING IN ENDLINE 		

//ANALYSIS - LEVEL 0

global outcomes_primary entrylevel_job salary_highest z_english
global outcomes_secondary english_job
global covariates female age college dad_edu mom_edu ses1 employed salary_estimate
		
foreach var of varlist $outcomes_primary $outcomes_secondary {
	areg `var' i.accepted, absorb(session_id) robust
	est store `var'1
	areg `var' i.accepted $covariates, absorb(session_id) robust
	est store `var'2
}	

outreg2 [entrylevel_job1 entrylevel_job2 salary_highest1 salary_highest2 z_english1 z_english2 english_job1 english_job2] using "fea_evaluation_results_L0.xls", bdec(3) excel replace

foreach var of varlist $outcomes_primary $outcomes_secondary {
	ivregress 2sls `var' i.session_id (inclass = accepted), vce(robust)
	est store `var'1
	ivregress 2sls `var' $covariates i.session_id (inclass = accepted), vce(robust)
	est store `var'2
}	

outreg2 [entrylevel_job1 entrylevel_job2 salary_highest1 salary_highest2 z_english1 z_english2 english_job1 english_job2] using "fea_evaluation_resultsIV_L0.xls", bdec(3) excel replace
	
//BALANCE CHECK

foreach var of varlist $covariates {
	areg `var' i.accepted, absorb(session_id) robust
	est store `var'
}

outreg2 [$covariates] using "fea_evaluation_balance_L0.xls", bdec(3) excel replace

areg accepted $covariates, absorb(session_id) robust

//ATTRITION CHECK

generate attrited = 0 if entrylevel_job != .
replace attrited = 1 if entrylevel_job == .

foreach var of varlist $covariates {
	areg `var' i.accepted##i.attrited, absorb(session_id) robust
	est store `var'
}

outreg2 [$covariates] using "fea_evaluation_attrition_L0.xls", bdec(3) excel replace

//ANALYSIS - LEVEL 1

preserve
keep if level1 == 1

foreach var of varlist $outcomes_primary $outcomes_secondary {
	areg `var' i. accepted, absorb(session_id) robust
	est store `var'1
	areg `var' i.accepted $covariates, absorb(session_id) robust
	est store `var'2
}	

outreg2 [entrylevel_job1 entrylevel_job2 salary_highest1 salary_highest2 z_english1 z_english2 english_job1 english_job2] using "fea_evaluation_results_L1.xls", bdec(3) excel replace
	
foreach var of varlist $outcomes_primary $outcomes_secondary {
	ivregress 2sls `var' i.session_id (inclass_attendance = accepted), vce(robust)
	est store `var'1
	ivregress 2sls `var' $covariates i.session_id (inclass_attendance = accepted), vce(robust)
	est store `var'2
}	

outreg2 [entrylevel_job1 entrylevel_job2 salary_highest1 salary_highest2 z_english1 z_english2 english_job1 english_job2] using "fea_evaluation_resultsIV_L1.xls", bdec(3) excel replace
	
//BALANCE CHECK

foreach var of varlist $covariates {
	areg `var' i.accepted, absorb(session_id) robust
	est store `var'
}

outreg2 [$covariates] using "fea_evaluation_balance_L1.xls", bdec(3) excel replace

//ATTRITION CHECK

foreach var of varlist $covariates {
	areg `var' i.accepted##i.attrited, absorb(session_id) robust
	est store `var'
}

outreg2 [$covariates] using "fea_evaluation_attrition_L1.xls", bdec(3) excel replace

restore

//ANALYSIS - LEVEL 2

preserve
keep if level2 == 1

foreach var of varlist $outcomes_primary $outcomes_secondary {
	areg `var' i. accepted, absorb(session_id) robust
	est store `var'1
	areg `var' i.accepted $covariates, absorb(session_id) robust
	est store `var'2
}	

outreg2 [entrylevel_job1 entrylevel_job2 salary_highest1 salary_highest2 z_english1 z_english2 english_job1 english_job2] using "fea_evaluation_results_L2.xls", bdec(3) excel replace

foreach var of varlist $outcomes_primary $outcomes_secondary {
	ivregress 2sls `var' i.session_id (inclass_attendance = accepted), vce(robust)
	est store `var'1
	ivregress 2sls `var' $covariates i.session_id (inclass_attendance = accepted), vce(robust)
	est store `var'2
}	

outreg2 [entrylevel_job1 entrylevel_job2 salary_highest1 salary_highest2 z_english1 z_english2 english_job1 english_job2] using "fea_evaluation_resultsIV_L2.xls", bdec(3) excel replace
	
//BALANCE CHECK

foreach var of varlist $covariates {
	areg `var' i.accepted, absorb(session_id) robust
	est store `var'
}

outreg2 [$covariates] using "fea_evaluation_balance_L2.xls", bdec(3) excel replace

//ATTRITION CHECK

foreach var of varlist $covariates {
	areg `var' i.accepted##i.attrited, absorb(session_id) robust
	est store `var'
}

outreg2 [$covariates] using "fea_evaluation_attrition_L2.xls", bdec(3) excel replace

restore

//ANALYSIS - LEVEL 3

preserve
keep if level3 == 1

foreach var of varlist $outcomes_primary $outcomes_secondary {
	areg `var' i. accepted, absorb(session_id) robust
	est store `var'1
	areg `var' i.accepted $covariates, absorb(session_id) robust
	est store `var'2
}	

outreg2 [entrylevel_job1 entrylevel_job2 salary_highest1 salary_highest2 z_english1 z_english2 english_job1 english_job2] using "fea_evaluation_results_L3.xls", bdec(3) excel replace

foreach var of varlist $outcomes_primary $outcomes_secondary {
	ivregress 2sls `var' i.session_id (inclass_attendance = accepted), vce(robust)
	est store `var'1
	ivregress 2sls `var' $covariates i.session_id (inclass_attendance = accepted), vce(robust)
	est store `var'2
}	

outreg2 [entrylevel_job1 entrylevel_job2 salary_highest1 salary_highest2 z_english1 z_english2 english_job1 english_job2] using "fea_evaluation_resultsIV_L3.xls", bdec(3) excel replace
	
//BALANCE CHECK

foreach var of varlist $covariates {
	areg `var' i.accepted, absorb(session_id) robust
	est store `var'
}

outreg2 [$covariates] using "fea_evaluation_balance_L3.xls", bdec(3) excel replace

//ATTRITION CHECK

foreach var of varlist $covariates {
	areg `var' i.accepted##i.attrited, absorb(session_id) robust
	est store `var'
}

outreg2 [$covariates] using "fea_evaluation_attrition_L3.xls", bdec(3) excel replace

restore

//HETEROGENEOUS EFFECTS ANALYSES

//FEMALE
foreach var of varlist $outcomes_primary $outcomes_secondary {
	areg `var' i.accepted##i.female, absorb(session_id) robust
	est store `var'1
	areg `var' i.accepted##i.female $covariates, absorb(session_id) robust
	est store `var'2
}	

outreg2 [entrylevel_job1 entrylevel_job2 salary_highest1 salary_highest2 z_english1 z_english2 english_job1 english_job2] using "fea_evaluation_results_female.xls", bdec(3) excel replace

//MOM's EDU

foreach var of varlist $outcomes_primary $outcomes_secondary {
	areg `var' i.accepted##i.mom_edu, absorb(session_id) robust
	est store `var'1
	areg `var' i.accepted##i.mom_edu $covariates, absorb(session_id) robust
	est store `var'2
}	

outreg2 [entrylevel_job1 entrylevel_job2 salary_highest1 salary_highest2 z_english1 z_english2 english_job1 english_job2] using "fea_evaluation_results_momedu.xls", bdec(3) excel replace

//EMPLOYED
foreach var of varlist $outcomes_primary $outcomes_secondary {
	areg `var' i.accepted##i.employed, absorb(session_id) robust
	est store `var'1
	areg `var' i.accepted##i.employed $covariates, absorb(session_id) robust
	est store `var'2
}	

outreg2 [entrylevel_job1 entrylevel_job2 salary_highest1 salary_highest2 z_english1 z_english2 english_job1 english_job2] using "fea_evaluation_results_employed.xls", bdec(3) excel replace


//CAUSAL MEDIATION ANALYSIS

medeff (regress z_english accepted $covariates) (regress entrylevel_job accepted z_english $covariates) , treat(accepted) mediate(z_english) sims(1000) seed(1)

medeff (regress z_english accepted $covariates) (regress salary_highest accepted z_english $covariates) , treat(accepted) mediate(z_english) sims(1000) seed(1)

*********************************
*Figure Xs: quantile regressions*
*********************************


gen treat = .
gen SE = .
gen percentile=_n/100

tab session_id, generate(session_id)

qreg2 salary_highest accepted $covariates session_id*, quantile(10)
est store a
qreg2 salary_highest accepted $covariates session_id*, quantile(25)
est store b
qreg2 salary_highest accepted $covariates session_id*, quantile(50)
est store c
qreg2 salary_highest accepted $covariates session_id*, quantile(75)
est store d
qreg2 salary_highest accepted $covariates session_id*, quantile(90)
est store e

outreg2 [a b c d e] using quantile_salary.xls, excel replace bdec(3)

local i 1
while `i' <= 99 {
	local j=`i'/100
	quietly qreg2 salary_highest accepted $covariates session_id*, quantile(`j')
	mat beta=e(b)
	mat var=e(V)
	quietly replace treat=beta[1,1] if _n==`i'
	quietly replace SE=var[1,1]^.5 if _n==`i'
	di `i'
	local i = `i' + 1
	}
gen lower=treat-1.96*SE
gen upper=treat+1.96*SE

twoway (scatter treat percentile if _n>=5 & _n<=95, connect(l))(rcapsym lower upper percentile if _n>=5 & _n<=95, msymbol(i)), xlabel(0 (0.10) 1) ylabel(-0.5 (0.1) 0.5) xtitle("percentile") ytitle("Estimated Treatment Effect") legend(order(1 "Estimated Treatment Effect" 2 "95% CI")) title("Appendix Figure 1A: Quantile Treatment Estimates for Salary per Month", size(medium)) saving("QTE_Monthly Salary", replace)

drop treat SE percentile lower upper

gen treat = .
gen SE = .
gen percentile=_n/100

qreg2 z_english accepted $covariates session_id*, quantile(10)
est store a
qreg2 z_english accepted $covariates session_id*, quantile(25)
est store b
qreg2 z_english accepted $covariates session_id*, quantile(50)
est store c
qreg2 z_english accepted $covariates session_id*, quantile(75)
est store d
qreg2 z_english accepted $covariates session_id*, quantile(90)
est store e

outreg2 [a b c d e] using quantile_english.xls, excel replace bdec(3)

local i 1
while `i' <= 99 {
	local j=`i'/100
	quietly qreg2 z_english accepted $covariates session_id*, quantile(`j')
	mat beta=e(b)
	mat var=e(V)
	quietly replace treat=beta[1,1] if _n==`i'
	quietly replace SE=var[1,1]^.5 if _n==`i'
	di `i'
	local i = `i' + 1
	}
gen lower=treat-1.96*SE
gen upper=treat+1.96*SE

twoway (scatter treat percentile if _n>=5 & _n<=95, connect(l))(rcapsym lower upper percentile if _n>=5 & _n<=95, msymbol(i)), xlabel(0 (0.10) 1) ylabel(-0.5 (0.1) 0.5) xtitle("percentile") ytitle("Estimated Treatment Effect") legend(order(1 "Estimated Treatment Effect" 2 "95% CI")) title("Appendix Figure 1A: Quantile Treatment Estimates for English Score", size(medium)) saving("QTE_English Score", replace)
